
var bgfloat="<div id=\"BoxBg\" style=\"height:"+$(document).height()+"px;filter:alpha(opacity=0);opacity:0;\"></div>";

var listDiv = '<div class = "close"><span><a onclick="closeDiv(\'listDiv\');" href="javascript:void(0);"><img width="17px"height="17px"src="../images/close.jpg"/></a></span></div> 密码帐号：<span id="account"></span>';
listDiv += '<div class="pmsCommon"><h4><a id="pmsListBtn" href="javascript:;">权限列表</a> | <a id="pmsAddBtn" href="javascript:;">增加用户</a></h4><ul></ul></div>';
listDiv += '<div id="pmsList" class="pmsList" style="display:block;" ><table class="tablelist"><tr></tr></table></div>';
listDiv += '<div id="pmsAdd"class="pmsAdd" style="display:none"><li>用户：<input class="username"  type="text" id="username" name="username" value="">';
listDiv += '<img id="flbtn" class="pointer fl" style="padding-top:3px;" alt="选择用户" src="../images/xz1.gif"/>';
listDiv += '<input type="checkbox" id="chooseAll" class="checkbox"><span class="allfont">全选</span><div class="serverlist" id="userlist" style="display:none">';	
listDiv += '<div class="fbar" id="fbar">请选择用户</div><ul></ul></div></li><li><input type="button" id="addUserBtn" value="提 交" class="btn" /></li></ul></div>';

var allvalue = '';
var allpasswdId = '';

// listDiv +=' <div id="pmsList" class="pmsList"><h4>权限列表</h4><ul></ul></div>';
function showComDiv(divhtml,divname,act,value)
{	
	var popdiv = divname;
	allvalue = value;

	addDiv(popdiv,popdiv);
	document.getElementById(popdiv).innerHTML=divhtml; 			


	$("#"+popdiv).before(bgfloat);
	$("#BoxBg").addClass("floatBg");

	$("#"+popdiv).css({top:($(document).scrollTop()+120)+"px"});
	$("#BoxBg").show();
	$("#BoxBg").css({opacity:"0.2","height": wHeight()});

	chooseAll();			//全选事件
	addPrivilege(value);	//ajax事件
	showDiv(act,value);
	
	
}
function showDiv(act,value)
{
	if(value != "" && typeof(value) != "undefined" && value.indexOf(",") != -1)
	{
		var nameValue = value.substring(0,value.indexOf(","));
		var idValue =  value.substring(value.indexOf(",")+1,value.length);
		$("#account").html(nameValue);
	}else{
		$("#account").html(传递帐号错误);
	}
	allpasswdId = idValue;
	if(act == 'pmsadd')//增加权限
	{
		$("#pmsListBtn").click(function(value){
			$("#pmsList").css("display","block");
			$("#pmsAdd").css("display","none");
			$(".pagenav").css("display","block");;
			showDiv('pmsList',value);
		});
	}
	else if(act == 'pmsList')
	{
		if(value != "" && value != undefined )
		{
			showPage(idValue,value);
			listPermission(idValue,value);	
			$("#pmsAddBtn").click(function(){
			$("#pmsList").css("display","none");
			$("#pmsAdd").css("display","block");
			$(".pagenav").css("display","none");;
			showDiv('pmsadd',value);
		});
		}
		else
		{
			alert("没有传递权限id");
		}
		
	}
}
function closePmsDiv()
{
	if($("#pms").css("display") != 'none')
	{
		$("#pms").css("display","none");
		$("#BoxBg").remove();
	}
	if($("#listDiv").css("display") != 'none')
	{
		$("#listDiv").css("display","none");
		$("#BoxBg").remove();
	}
}
function showPage(passwdid,value)
{
	var theoffset = '';
	$("#listDiv .showpage a").die().live("click",function()
	{
		$(".pagenav").remove();
		var thelink = this.href;
		if(thelink != "")
		{
			var pstar = thelink.indexOf("#")+1;
			theoffset = thelink.substring(pstar);
			theoffset = theoffset.replace("-","");
			if(theoffset == '')
				theoffset = 0;
			listPermission(passwdid,value,theoffset);
		}
	})
	
	

	$("#listDiv .showpage #gotopage").die().live("keydown",function(e)
	{
		if(e.keyCode == 13)
		{
			var topage = parseInt($("#listDiv .showpage #gotopage").val());
			var pernum = $("#listDiv .showpage #gotopage").attr("maxlength");
			
			if(topage<=parseInt(this.name))
			{
				topage = topage - 1;
				if(topage>=0)
				{
					listPermission(passwdid,value,topage*pernum);
				}
			}
		}
	})

	//makeGoto(passwdid,value);
}
var theOffset = 0;
function makeGoto()
{
	var topage = parseInt($(".showpage #gotopage").val());
	var pernum = $(".showpage #gotopage").attr("maxlength");
	var maxPage = parseInt($("#gotopage").attr('name'));	
	if(topage<=maxPage)
	{
		topage = topage - 1;
		theOffset = topage*pernum;
		
		if(topage>=0)
		{
			listPermission(allpasswdId,allvalue,theOffset);
		}

	}
	else
	{
		alert("请输入正确的页码");
		listPermission(allpasswdId,allvalue,theOffset);
	}

}



function listPermission(passwdid,value,offset)
{
	if(typeof offset == 'undefined')
	{
		var offset = 0;
	}

	var act = 'pmsList';
	$.ajax
	({
		type: "post",
		data: {"act":act,"passwdId":passwdid,"offset":offset},
		url: "../ajaxlist.php",
		dataType:"json",
	//	timeout: ajaxtimeout,
		beforeSend:function()
		{
			
		},
		success: function(text)
		{
			$("#pmsList table").empty();
			if(text.all_user)
			{
				$("#pmsList table").append("<tr><td width=20%>id</td><td width=50%>用户<font color='red'>(当前为所有用户)</font></td><td width=30%>操作</td>");
			}else{
				$("#pmsList table").append("<tr><td width=20%>id</td><td width=50%>用户</td><td width=30%>操作</td>");	
			}
			
			if(text.status == 1)
			{
				for(var i in text.info)
				{
					var nameValue = value.substring(0,value.indexOf(","));
					var idValue =  value.substring(value.indexOf(",")+1,value.length);
					$("#pmsList table").append("<tr><td>"+text.info[i].id+"</td><td>"+text.info[i].username+"</td><td><a onclick=\'delPms("+text.info[i].id+",\""+nameValue+"\","+idValue+");\'>删除</a></td></tr>");
				}
				$("#listDiv").append("<div class='pagenav'>"+text.page+"</div>");

			}
			else if(text.status == 0)
			{
				$("#pmsList table").append("<tr><td></td><td>"+text.info+"</td></tr>");
			}
			else
			{
				alert(text);
			}
		},
		error: function(){}
	})
 return false;
	
}
function addDiv(divid,css,before)
{
	if(before == undefined || before == "")
		before = "body";
	$(before).append("<div id='"+divid+"' class='"+css+"' ></div>");	
}

function closeDiv(div)
{
	$("#"+div).remove();
	$("#BoxBg").remove();
}
//获取页面高度
function wHeight(){
	var _sh=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	var _ch=Math.max(document.body.clientHeight,document.documentElement.clientHeight);
	return Math.max(_sh,_ch);
}

function addPrivilege(value)
{
	var nameValue = value.substring(0,value.indexOf(","));
		var idValue =  value.substring(value.indexOf(",")+1,value.length);
	$("#addUserBtn").click(function(){
	var username = $("#username").val();
	if(username == "")
	{
		alert("请选择用户再提交！");
		return false;
	}

	var act = 'addPrivilege';
	$.ajax
	({
		type: "post",
		data: {"act":act,"username":username,"passwdId":idValue},
		url: "../ajaxlist.php",
		dataType:"json",
	//	timeout: ajaxtimeout,
		beforeSend:function()
		{
			
		},
		success: function(text)
		{
			if(text.status == 1)
			{
				alert(text.info);
				closeDiv('listDiv');
				showComDiv(listDiv,'listDiv','pmsList',value);
			}
			else if(text.status == 0)
			{
				alert(text.info);
			}
			else if(text.status == 2)
			{
				closeDiv('listDiv');
				showComDiv(listDiv,'listDiv','pmsList',value);
			}
			else
			{
				alert(text);
			}
		},
		error: function(){}
	})

	
	return false;
});
}

function chooseAll()
{
	
	$("#chooseAll").click(function(){		
		if(this.checked){
			var act = 'userlist';
			$.ajax
			({
				type: "post",
				data: {"act":act},
				url: "../ajaxlist.php",
				dataType:"json",
			//	timeout: ajaxtimeout,
				beforeSend:function()
				{
					
				},
				success: function(text)
				{
					if(text)
					{
						for(var i in text)
					    {
					    	var username_change = $("#username").val();
					    	if(username_change=="")
								$("#username").val(text[i].username);
							else
							{
								if(!checkName(username_change,text[i].username))
								{
									$("#username").val(username_change+","+text[i].username);
								}
								//if($("#username").val().indexOf(text[i].username) == -1)	
									
							}
						}
					}
					else
					{
						$("#fbar").html("当前没有用户");
					}
				},
				error: function(){}
			});
		}else{
			$("#username").val("");
		}
	});

}

function checkName(val,name)
{
	var flag = false;
	if(val != "" && val != undefined)
	{
		var username = val.split(",");
		for(var i in username)
		{
			if(username[i] == name)
				flag = true;
		}
	}

	return flag;
}

function delPms(pmsId,psName,psId)
{	
	if(psName != '' && psId != '')
	{
		value = psName+','+psId;
	}
	else
	{
		alert('传递参数错误,请检查代码');
		return false;
	}

	if(confirm("确定要删除吗？"))
	{
		if(pmsId != "" && pmsId != undefined)
		{
			var act = 'delPms';
			$.ajax
			({
				type: "post",
				data: {"act":act,"pmsId":pmsId},
				url: "../ajaxlist.php",
				dataType:"json",
			//	timeout: ajaxtimeout,
				beforeSend:function()
				{
					
				},
				success: function(text)
				{
					if(text)
					{
						if(text.status == 1)
						{
							closeDiv('listDiv');
							showComDiv(listDiv,'listDiv','pmsList',value);
						}
						else
						{
							alert(text.info);
						}
					}
					else
					{
						alert(text.info);
					}
				},
				error: function(){}
			});
		}
	}
	
}